import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Solution1030 {
    public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
        List<int[]> list=new ArrayList<>();
        for (int i=0;i<rows;i++){
            for (int j=0;j<cols;j++){
                list.add(new int[]{i,j,Math.abs(i-rCenter)+Math.abs(j-cCenter)});
            }
        }
        list.sort(Comparator.comparingInt(a -> a[2]));
        int[][] ans=new int[list.size()][2];
        for (int i=0;i<list.size();i++){
            ans[i][0]=list.get(i)[0];
            ans[i][1]=list.get(i)[1];
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][] ans=new Solution1030().allCellsDistOrder(2,2,0,1);
        for (int[] an : ans) {
            System.out.println(Arrays.toString(an));
        }
    }
}
